<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">

    function test_Kinetic (t) {
        t.plan(17);
        var finish = false;
        var results = {
            110: {x: -2.7, y: -3.6, end: false},
            120: {x: -2.1, y: -2.8, end: false},
            130: {x: -1.5, y: -2.0, end: false},
            140: {x: -0.9, y: -1.2, end: false},
            150: {x: -0.3, y: -0.4, end: true}
        };

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };
        
        var interval = 10; // arbitrary value for tests 

        var originalLoopAnimation = OpenLayers.Animation.start;
        OpenLayers.Animation.start = function(callback) {
            while (!finish) {
                var time = new Date().getTime();
                Date.prototype.getTime = function() { return time+interval };
                callback();
            }
        };

        var kinetic = new OpenLayers.Kinetic({
            deceleration: 0.01
        });
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:40});

        t.eq(measure.speed, 0.5, "correct speed");
        t.eq(measure.theta, Math.PI - Math.atan(40/30), "correct angle");

        // fake timer id    
        kinetic.timerId = 0;
        kinetic.move(measure, function(x, y, end) {
            var result = results[new Date().getTime()];
            t.eq(Math.round(x * 1000) / 1000, result.x, "correct x");
            t.eq(Math.round(y * 1000) / 1000, result.y, "correct y");
            t.eq(end, result.end, "correct end");
            finish = end;
        });
        
        Date.prototype.getTime = originalGetTime;
        OpenLayers.Animation.start = originalLoopAnimation;
    }

    function test_Angle (t) {
        t.plan(8);
        var results = [
            {speed: 0.5, theta: Math.round((Math.PI - Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((Math.PI + Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((- Math.atan(40/30)) * 1000000) / 1000000},
            {speed: 0.5, theta: Math.round((Math.atan(40/30)) * 1000000) / 1000000}
        ];

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:40});

        t.eq(measure.speed, results[0].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[0].theta, "correct angle");


        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:30, y:-40});

        t.eq(measure.speed, results[1].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[1].theta, "correct angle");


        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:-30, y:-40});

        t.eq(measure.speed, results[2].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[2].theta, "correct angle");

        var originalGetTime = Date.prototype.getTime;
        Date.prototype.getTime = function() { return 0 };

        var kinetic = new OpenLayers.Kinetic();
        kinetic.begin();
        kinetic.update({x:0, y:0});
        
        Date.prototype.getTime = function() { return 100 };
        var measure = kinetic.end({x:-30, y:40});

        t.eq(measure.speed, results[3].speed, "correct speed");
        t.eq(Math.round(measure.theta * 1000000) / 1000000,
                results[3].theta, "correct angle");

        Date.prototype.getTime = originalGetTime;
    }
  </script>
</head>
<body>
    <div id="map" style="width: 600px; height: 300px;"/>
    <div style="display: none;"><div id="invisimap"></div></div>
</body>
</html>
